<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Counter</title>
</head>
<body>
  <h1 id="counter"></h1>
  <script>
    const key = 'counter'
    let count = JSON.parse(
      sessionStorage.getItem(key)
    ) || 0 // 读取，从sessionStorage里取出来
    function render() {
      document.querySelector('#counter').innerText
        = count
    }
    document.documentElement.onclick = () => {
      count += 1 // 发生改变，要保存到sessionStorage
      sessionStorage.setItem(key,
                           JSON.stringify(count))
      render()
    }
    render()
  </script>
</body>
</html>